package com.lynch.lrc;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * @Function：
 * @Author:Golden Zhao
 * @Version:
 * @Since:
 */
public class LrcProcessor {

	public ArrayList<Queue> process(InputStream inputStream){
		//存放时间点数据
		Queue<Long> timeMills=new LinkedList<Long>();
		//存放时间点所对应的歌词
		Queue<String> messages =new LinkedList<String>();
		ArrayList<Queue> queues=new ArrayList<Queue>();
		try {
			System.out.println("try前");
			//创建BufferedReader对象
			InputStreamReader inputReader = new InputStreamReader(inputStream);
			BufferedReader bufferedReader = new BufferedReader(inputReader);
			String temp = null;
			int i=0;
			//匹配两遍都带中括号的
			Pattern p = Pattern.compile("\\[([^\\]]+)\\]");
			String result = null;
			boolean b = true;
			System.out.println("while前");
			while ((temp=bufferedReader.readLine())!=null) {
				i++;
				System.out.println("i=========" + temp);
				Matcher m = p.matcher(temp);
				if (m.find()) {
					if(result!=null){
						messages.add(result);
					}
					String timeStr = m.group();
					Long timeMill = time2Long(timeStr.substring(1,timeStr.length()-1));
					if (b) {
						timeMills.offer(timeMill);
					}
					String msg = temp.substring(10);
					result = "" + msg + "\n";
				}else {
					result = result + temp + "\n";
				}
			}
			System.out.println("while后");
			messages.add(result);
			queues.add(timeMills);
			queues.add(messages);
			System.out.println("打印前");
			System.out.println("result: "+result);
			System.out.println("----------------------------------------------");
			System.out.println("time mills: "+timeMills);
			System.out.println("----------------------------------------------");
			System.out.println("messages: "+messages);
		} catch (Exception e) {
			System.out.println("解析的时候出错");
			e.printStackTrace();
		}
		return queues;
	}
	
	public Long time2Long(String timeStr) {
		String s[]=timeStr.split(":");
		int min=Integer.parseInt(s[0]);
		String ss[]=s[1].split("\\.");
		int sec=Integer.parseInt(ss[0]);
		int mill=Integer.parseInt(ss[1]);
		return min*60*1000+sec*1000+mill*10L;
	}
}
